package com.bootdo.program.wallpaper.controller;

import com.bootdo.common.config.Constant;
import com.bootdo.common.controller.BaseController;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.program.wallpaper.domain.BzBaseinfoDO;
import com.bootdo.program.wallpaper.service.BzBaseinfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 小程序基础配置
 * 
 * @author guoyunfei
 * @email 2549789059@qq.com
 * @date 2021-08-12 21:59:33
 */
 
@Controller
@RequestMapping("/wallpaper/bzBaseinfo")
public class BzBaseinfoController extends BaseController {
	@Autowired
	private BzBaseinfoService bzBaseinfoService;
	
	@GetMapping()
	@RequiresPermissions("wallpaper:bzBaseinfo:bzBaseinfo")
	String BzBaseinfo(){
	    return "wallpaper/bzBaseinfo/bzBaseinfo";
	}


	@GetMapping("/get")
	@ResponseBody
	R get (){
		String appid = isAdmin() ? "wxe763c88aa16d0565" : (null == defaultAppid() ? "noData" : defaultAppid());
		BzBaseinfoDO baseinfoDO = bzBaseinfoService.getByAppid(appid);
		Map<String,Object> map = new HashMap<>();
		map.put("baseinfoDO",baseinfoDO);
		return R.ok(map);
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wallpaper:bzBaseinfo:edit")
	public R update( BzBaseinfoDO bzBaseinfo){
		if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
			return R.error(1, "演示系统不允许修改,完整体验请部署程序");
		}
		if(null == defaultAppid()){
			return R.error("保存失败，请先配置小程序appid");
		}
		bzBaseinfo.setAppid(defaultAppid());
		if(null == bzBaseinfo.getId()){
			if(StringUtils.isNotBlank(bzBaseinfo.getAppBg())){
				bzBaseinfo.setAppBg(getHttpUrl() + bzBaseinfo.getAppBg());
			}
			if(StringUtils.isNotBlank(bzBaseinfo.getShareImg())){
				bzBaseinfo.setShareImg(getHttpUrl() + bzBaseinfo.getShareImg());
			}
			if(StringUtils.isNotBlank(bzBaseinfo.getAppLogo())){
				bzBaseinfo.setAppLogo(getHttpUrl() + bzBaseinfo.getAppLogo());
			}
			bzBaseinfoService.save(bzBaseinfo);
			return R.ok();
		}

		if(bzBaseinfo.getAppBg().indexOf("http") != -1){
			bzBaseinfo.setAppBg(null != bzBaseinfo.getAppBg() && !"".equals(bzBaseinfo.getAppBg()) ? bzBaseinfo.getAppBg() : null);
		}else{
			bzBaseinfo.setAppBg(null != bzBaseinfo.getAppBg() && !"".equals(bzBaseinfo.getAppBg()) ? getHttpUrl() + bzBaseinfo.getAppBg() : null);
		}
		if(bzBaseinfo.getShareImg().indexOf("http") != -1){
			bzBaseinfo.setShareImg(null != bzBaseinfo.getShareImg() && !"".equals(bzBaseinfo.getShareImg()) ? bzBaseinfo.getShareImg() : null);
		}else{
			bzBaseinfo.setShareImg(null != bzBaseinfo.getShareImg() && !"".equals(bzBaseinfo.getShareImg()) ? getHttpUrl() + bzBaseinfo.getShareImg() : null);
		}
		if(bzBaseinfo.getAppLogo().indexOf("http") != -1){
			bzBaseinfo.setAppLogo(null != bzBaseinfo.getAppLogo() && !"".equals(bzBaseinfo.getAppLogo()) ? bzBaseinfo.getAppLogo() : null);
		}else{
			bzBaseinfo.setAppLogo(null != bzBaseinfo.getAppLogo() && !"".equals(bzBaseinfo.getAppLogo()) ? getHttpUrl() + bzBaseinfo.getAppLogo() : null);
		}




		bzBaseinfoService.update(bzBaseinfo);
		return R.ok();
	}
	

}
